package edu.gyc.data.struct;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class HiYsf {
    public static void ysf(int n, int k, int start) {
        int[] circle = new int[n];
        int count=1;
        int i=start;
        int total=0;
        boolean flag=true;
        while (flag) {
            if (circle[i] == 0) {
                count++;
            }
            if (count == k) {
                circle[i]=1;
                total++;
                count=0;
                System.out.printf("%d\t",(i+1));
                if (total == n) {
                    flag=false;
                }
            }
            i++;
            if (i >= n) {
                i%=n;
            }
        }
        System.out.println();
    }
    public static void ysfLinkList(int n, int k) {
        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            list.addLast(i+1);
        }
        int index=0;
        while (list.size() >1) {
            for (int i = 0; i < k-1; i++) {
                if (index == list.size() - 1) {
                    index=0;

                }else {
                    index++;
                }

            }
            int r=   list.remove(index);
            System.out.printf("%d\t",r);
        }
       System.out.println(list.get(0));
    }
    public static void ysfLinkList1(int n, int k,int start) {
        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            list.addLast(i+1);
        }
        int index=start-1;
        while (list.size() >0) {
            index=(index+k-1)%list.size();
            int r=   list.remove(index);
            System.out.printf("%d\t",r);
        }
    }
    public static void main(String[] args) {
        ysf(10,3,3);
        ysfLinkList(10,3);
        ysfLinkList1(10,3,3);
    }
}
